//
//  Root_AppTVCaseController.m
//  classicFilms
//
//  Created by 刘继丹 on 17/4/24.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "Root_AppTVCaseController.h"
#import "AppTvCaseCCell.h"
#import "KeyHeader.h"
#import "ServiceListApi.h"
#import "AppTVCaseDetailController.h"
#import "CaseListApi.h"

@interface Root_AppTVCaseController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) UICollectionView *mCollectionView;

@property(nonatomic, strong) NSArray *mDataArr;

@end

@implementation Root_AppTVCaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kBackGroundColor_blue;

    [self.view addSubview:self.mCollectionView];
    
    [self loadNewData];
    
}

-(void)loadNewData{
   
    CaseListApi *caseListApi = [[CaseListApi alloc]init];
    
    [caseListApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"======%@", request.responseJSONObject);
        _mDataArr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"];
        [_mCollectionView reloadData];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    
}

-(UICollectionView *)mCollectionView{
    if (!_mCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _mCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KVIEW_W, KVIEW_H) collectionViewLayout:layout];
        _mCollectionView.delegate = self;
        _mCollectionView.dataSource = self;
        _mCollectionView.showsHorizontalScrollIndicator = YES;
        _mCollectionView.backgroundColor = [UIColor clearColor];
        _mCollectionView.remembersLastFocusedIndexPath = YES;
        [_mCollectionView setContentInset:UIEdgeInsetsMake(50+KTABBAR_H, 100, 50, 100)];
        [_mCollectionView registerClass:[AppTvCaseCCell class] forCellWithReuseIdentifier:@"AppTvCaseCCell"];
    }
    return _mCollectionView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_mDataArr count];
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 50;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 50;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((KVIEW_W-300)/3.f, 312);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AppTvCaseCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppTvCaseCCell" forIndexPath:indexPath];
    NSDictionary *dic = [_mDataArr objectAtIndex:indexPath.row];
    cell.dic = dic;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AppTVCaseDetailController *detail = [[AppTVCaseDetailController alloc]init];
    NSDictionary *dic = [_mDataArr objectAtIndex:indexPath.row];
    detail.caseId = [dic valueForKey:@"id"];
    [self presentViewController:detail animated:YES completion:^{}];
}

- (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context {
    BOOL shouldUpdateFocusInContext = YES;
    if (context != nil) {
        if (context.focusHeading == UIFocusHeadingRight) {
            UIView *previouslyFocusedView = context.previouslyFocusedView;
            UIView *nextFocusedView = context.nextFocusedView;
            if (previouslyFocusedView != nil && nextFocusedView != nil && [previouslyFocusedView isEqual:nextFocusedView]) {
                shouldUpdateFocusInContext = NO;
            }
        }
    }
    return shouldUpdateFocusInContext;
}

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    
    [coordinator addCoordinatedAnimations:^{
        
        
    } completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
